package com.code;

import java.sql.ResultSet;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obTongMatHang;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class TK_TongMatHang extends Activity {

	private String strNhomChon = "";
	private String strKhoChon = "";
	ArrayList<Integer> date_actual;
	private String date;
	private LinearLayout llMainTongMatHang;
	private final int REQUESTCODE_DETAIL = 100;
	private TableRow rowFocus;
	private Integer choose_tmh = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tk_tongmathang);
		// declare component

		llMainTongMatHang = (LinearLayout) findViewById(R.id.lltongmathang);
		strNhomChon = super.getIntent().getExtras().getString("strNhomChon");
		strKhoChon = super.getIntent().getExtras().getString("strKhoChon");
		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_actual");
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		choose_tmh = super.getIntent().getExtras().getInt("choose_tmh");
		loadData(choose_tmh);
	}

	public void loadData(int choose) {
		Float totalThanhTien = 0.0f;
		Integer totalsoLuongXuat = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT Products.[Product ID] as ProductID, ");
		sql.append(" sum(vattuxuat.SL) as SL, ");
		sql.append(" SUM((vattuxuat.SL * vattuxuat.dongia) * (1 + ISNULL(vattuxuat.Thue, 0)))AS ThanhTien ");
		sql.append(" FROM Phieuxuat INNER JOIN vattuxuat ON Phieuxuat.SoCt = vattuxuat.SoCT INNER JOIN ");
		sql.append(" Products ON vattuxuat.[Product ID] = Products.[Product ID] ");
		if (!strKhoChon.equals(""))
			sql.append(" AND Phieuxuat.MSK COLLATE DATABASE_DEFAULT in (select value from dbo.fn_Split('"
					+ strKhoChon + "',','))");
//		if (choose == TK_ChonMau.CHOOSE_NAM)
			sql.append(" AND DATEPART(YYYY,Phieuxuat.Ngay) = DATEPART(YYYY,'"
					+ date + "')");
		 if (choose == TK_ChonMau.CHOOSE_QUI)
			sql.append(" AND DATEPART(quarter,Phieuxuat.Ngay) = DATEPART(quarter,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_THANG)
			sql.append(" AND DATEPART(MM,Phieuxuat.Ngay) = DATEPART(MM,'"
					+ date + "')");
		else if (choose == TK_ChonMau.CHOOSE_TUAN)
			sql.append(" AND DATEPART(WEEK,Phieuxuat.Ngay) = DATEPART(WEEK,'"
					+ date + "')");
		else if(choose == TK_ChonMau.CHOOSE_NGAY)
			sql.append(" AND CAST(Phieuxuat.Ngay as date) = CAST('"
					+ date + "' as date)");
		if (!strNhomChon.equals(""))
			sql.append(" AND Products.[Category ID] =  '" + strNhomChon + "'");
		sql.append(" GROUP BY Products.[Product ID] ");
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());

		final TableLayout table = new TableLayout(this);
		llMainTongMatHang.addView(table);
		TableRow trHead = new TableRow(this);
		trHead.setWeightSum(5.0f);

		TextView tvMaHang = new TextView(this);
		tvMaHang.setText("Mã hàng");
		setLayoutForTextView(tvMaHang, true, Gravity.CENTER, 10, 2.5f);

		TextView tvSLXuat = new TextView(this);
		tvSLXuat.setText("SLX");
		setLayoutForTextView(tvSLXuat, true, Gravity.CENTER, 10, 1.0f);

		TextView tvThanhTien = new TextView(this);
		tvThanhTien.setText("TT");
		setLayoutForTextView(tvThanhTien, true, Gravity.CENTER, 10, 1.5f,true);

		// TextView tvTon = new TextView(this);
		// tvTon.setText("Tồn");
		// setLayoutForTextView(tvTon, true, Gravity.CENTER, 5, 0.5f);

		trHead.addView(tvMaHang);
		trHead.addView(tvSLXuat);
		trHead.addView(tvThanhTien);
		// trHead.addView(tvTon);
		table.addView(trHead);
		try {
			while (rs.next()) {
				// set value for TongMatHang
				final obTongMatHang obTMH = new obTongMatHang();
				if (rs.getString("ProductID") != null)
					obTMH.setMaHang(rs.getString("ProductID"));
				if (rs.getInt("SL") != 0)
					obTMH.setSoLuongXuat(String.valueOf(rs.getInt("SL")));
				else
					obTMH.setSoLuongXuat("0");
				if (rs.getString("ThanhTien") != null)
					obTMH.setThanhTien(rs.getString("ThanhTien"));
				else
					obTMH.setThanhTien("0.0f");

				// add date in textview
				final TableRow trHeads = new TableRow(this);
				trHeads.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						try {
							if (rowFocus != null)
								rowFocus.setBackgroundColor(Color.TRANSPARENT);
							rowFocus = (TableRow) v;
							v.setBackgroundColor(Color.YELLOW);
						} catch (Exception e) {
						}
					}
				});
				trHeads.setOnLongClickListener(new OnLongClickListener() {
					public boolean onLongClick(View v) {
						if (!obTMH.getMaHang().equals("")) {
							Intent i = new Intent(TK_TongMatHang.this,
									DetailTongMatHang.class);
							Bundle b = new Bundle();
							b.putString("strNhomChonDetail", strNhomChon);
							b.putString("strKhoChonDetail", strKhoChon);
							b.putString("maHangDetail", obTMH.getMaHang());
							b.putIntegerArrayList("dateDetail", date_actual);
							i.putExtras(b);
							startActivityForResult(i, REQUESTCODE_DETAIL);
						}
						return false;
					}
				});
				trHeads.setWeightSum(5.0f);

				TextView tvMaHangs = new TextView(this);
				tvMaHangs.setText(obTMH.getMaHang());
				setLayoutForTextView(tvMaHangs, false, Gravity.LEFT, 10, 2.5f);

				TextView tvSLXuats = new TextView(this);
				tvSLXuats.setText(obTMH.getSoLuongXuat());
				setLayoutForTextView(tvSLXuats, false, Gravity.CENTER, 10, 1.0f);

				TextView tvThanhTiens = new TextView(this);
				tvThanhTiens.setText(Global.formatDecimal(Float.valueOf(obTMH
						.getThanhTien())));
				setLayoutForTextView(tvThanhTiens, false, Gravity.RIGHT, 10,
						1.5f,true);
				// tvThanhTiens.setGravity(Gravity.RIGHT);

				// TextView tvTons = new TextView(this);
				// tvTons.setText("0");
				// setLayoutForTextView(tvTons, true, Gravity.CENTER, 5, 0.5f);

				trHeads.addView(tvMaHangs);
				trHeads.addView(tvSLXuats);
				trHeads.addView(tvThanhTiens);
				// trHeads.addView(tvTons);
				table.addView(trHeads);

				totalsoLuongXuat = totalsoLuongXuat
						+ Integer.valueOf(obTMH.getSoLuongXuat());
				totalThanhTien = totalThanhTien
						+Float.valueOf(obTMH.getThanhTien());

			}
			// add date in textview
			TableRow trHeadEnd = new TableRow(this);
			trHeadEnd.setWeightSum(5.0f);

			TextView tvMaHangEnd = new TextView(this);
			tvMaHangEnd.setText("Tổng");
			setLayoutForTextView(tvMaHangEnd, false, Gravity.CENTER, 10, 2.5f);
			tvMaHangEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvSLXuatEnd = new TextView(this);
			tvSLXuatEnd.setText(totalsoLuongXuat.toString());
			setLayoutForTextView(tvSLXuatEnd, false, Gravity.CENTER, 10,1.0f);
			tvSLXuatEnd.setBackgroundResource(R.layout.border_topleftbottom);

			TextView tvThanhTienEnd = new TextView(this);
			tvThanhTienEnd.setText(Global.formatDecimal(totalThanhTien).toString());
			setLayoutForTextView(tvThanhTienEnd, false, Gravity.RIGHT, 10,
					1.5f,true);
			tvThanhTienEnd.setBackgroundResource(R.layout.border);
//			tvThanhTienEnd.setGravity(Gravity.RIGHT);

			// TextView tvTonEnd = new TextView(this);
			// tvTonEnd.setText("0");
			// setLayoutForTextView(tvTonEnd, false, Gravity.CENTER, 5, 0.5f);
			// tvTonEnd.setBackgroundResource(R.layout.border_topleftbottom);

			trHeadEnd.addView(tvMaHangEnd);
			trHeadEnd.addView(tvSLXuatEnd);
			trHeadEnd.addView(tvThanhTienEnd);
			// trHeadEnd.addView(tvTonEnd);
			table.addView(trHeadEnd);

			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void setLayoutForTextView(TextView tv, Boolean isBold,
			int gravity, int Width, Float weight) {
		LayoutParams params = new TableRow.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.weight = weight;
		tv.setLayoutParams(params);
		tv.setBackgroundResource(R.layout.border_topleft);
		tv.setPadding(2, 10, 2, 10);
		tv.setGravity(gravity);
		tv.setWidth(Width);
		tv.setTextSize(18);
//		Typeface tf = Typefaces.get(tv.getContext(), "sansation.ttf");
//		tv.setTypeface(tf);
		if (isBold)
			tv.setTypeface(null, Typeface.BOLD);
	}	
	
	
	public static void setLayoutForTextView(TextView tv, Boolean isBold,
			int gravity, int Width, Float weight, Boolean isTVRight) {
		LayoutParams params = new TableRow.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		params.weight = weight;
		tv.setLayoutParams(params);
		tv.setBackgroundResource(R.layout.border_topleftright);
		tv.setPadding(2, 10, 2, 10);
		tv.setGravity(gravity);
		tv.setWidth(Width);
		tv.setTextSize(18);
//		Typeface tf = Typefaces.get(tv.getContext(), "sansation.ttf");
//		tv.setTypeface(tf);
		if (isBold)
			tv.setTypeface(null, Typeface.BOLD);
	}
}
